package cn.iocoder.yudao.module.design.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.projectmanager.vo.*;
import cn.iocoder.yudao.module.design.convert.projectmanager.ProjectManagerConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.projectmanager.ProjectManagerDO;
import cn.iocoder.yudao.module.design.service.projectmanager.ProjectManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 认证项目经理")
@RestController
@RequestMapping("/design/project-manager")
@Validated
public class AppProjectManagerController {

    @Resource
    private ProjectManagerService projectManagerService;

    @PostMapping("/create")
    @Operation(summary ="创建认证项目经理")
    @PreAuthorize("@ss.hasPermission('design:project-manager:create')")
    public CommonResult<Long> createProjectManager(@Valid @RequestBody ProjectManagerCreateReqVO createReqVO) {
        return success(projectManagerService.createProjectManager(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新认证项目经理")
    @PreAuthorize("@ss.hasPermission('design:project-manager:update')")
    public CommonResult<Boolean> updateProjectManager(@Valid @RequestBody ProjectManagerUpdateReqVO updateReqVO) {
        projectManagerService.updateProjectManager(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除认证项目经理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:project-manager:delete')")
    public CommonResult<Boolean> deleteProjectManager(@RequestParam("id") Long id) {
        projectManagerService.deleteProjectManager(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得认证项目经理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:project-manager:query')")
    public CommonResult<ProjectManagerRespVO> getProjectManager(@RequestParam("id") Long id) {
        ProjectManagerDO projectManager = projectManagerService.getProjectManager(id);
        return success(ProjectManagerConvert.INSTANCE.convert(projectManager));
    }

    @GetMapping("/list")
    @Operation(summary ="获得认证项目经理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:project-manager:query')")
    public CommonResult<List<ProjectManagerRespVO>> getProjectManagerList(@RequestParam("ids") Collection<Long> ids) {
        List<ProjectManagerDO> list = projectManagerService.getProjectManagerList(ids);
        return success(ProjectManagerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得认证项目经理分页")
    @PreAuthorize("@ss.hasPermission('design:project-manager:query')")
    public CommonResult<PageResult<ProjectManagerRespVO>> getProjectManagerPage(@Valid ProjectManagerPageReqVO pageVO) {
        PageResult<ProjectManagerDO> pageResult = projectManagerService.getProjectManagerPage(pageVO);
        return success(ProjectManagerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出认证项目经理 Excel")
    @PreAuthorize("@ss.hasPermission('design:project-manager:export')")
   
    public void exportProjectManagerExcel(@Valid ProjectManagerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProjectManagerDO> list = projectManagerService.getProjectManagerList(exportReqVO);
        // 导出 Excel
        List<ProjectManagerExcelVO> datas = ProjectManagerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "认证项目经理.xls", "数据", ProjectManagerExcelVO.class, datas);
    }

}
